<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        let p1  = new Promise((resolve,reject)=>{
            setTimeout(() => {
                resolve(111)
            }, 1000);
        })

        let p2  = new Promise((resolve,reject)=>{
            setTimeout(() => {
                resolve(222)
                // reject("error");
            }, 100);
        })

        let p3  = new Promise((resolve,reject)=>{
            setTimeout(() => {
                resolve(333)
            }, 50);
        })
        // ---------------原有静态方法-------------------------------------------------------
        // Promise.race() 不管是成功状态还是失败状态 谁执行最快就 返回谁的 PromiseResult
        Promise.race([p1,p2,p3]).then(res =>{
            console.log(res);  // 执行最快的结果 111
        })
        // ---------------重写底层----------------------------------------------------------
        function myRace(arr) {
            return new Promise((resolve,reject) => {
                arr.forEach(p => {
                    p.then(res=>{
                        // 成功状态 直接 resolve() 返回出去 就不执行了
                        resolve(p)
                    }).catch(err =>{
                        // 失败状态 直接 resolve() 返回出去 就不执行了
                        resolve(p)
                    })
                });
            })
            
        }
        myRace([p1,p2,p3]).then(res =>{
            console.log(res);
        })
    </script>
</body>
</html>